package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {

    @Autowired
    IWmNewsService wmNewsService;

    @Override
    public ResponseResult listByName(ChannelDto dto) {

        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(dto.getName())) {

            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        IPage<WmChannel> page1 = this.page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult saveChannel(WmChannel entity) {

        if (entity == null || StringUtils.isEmpty(entity.getName())) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(WmChannel::getName, entity.getName());

        WmChannel wmChannel = this.getOne(queryWrapper);

        if (wmChannel != null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        entity.setCreatedTime(new Date());

        this.save(entity);

        return ResponseResult.okResult(entity.getId());
    }

    @Override
    public ResponseResult updateChannel(WmChannel entity) {

        //   System.out.println("频道id是：" + com.heima.search.entity.getId());

        if (entity == null || StringUtils.isEmpty(entity.getName())) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();

        query.eq(WmChannel::getId,entity.getId());

        query.eq(WmChannel::getName,entity.getName());

        WmChannel one1 = this.getOne(query);

        if(one1==null){

            LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.like(WmChannel::getName, entity.getName());

            WmChannel wmChannel = this.getOne(queryWrapper);

            if (wmChannel != null) {

                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        if (entity.getStatus() == false) {

            LambdaQueryWrapper<WmNews> query1 = new LambdaQueryWrapper<>();

          //  System.out.println("频道是："+com.heima.search.entity.getId());

            query1.eq(WmNews::getChannelId, entity.getId());

            List<WmNews> list = wmNewsService.list(query1);

            if (!CollectionUtils.isEmpty(list)) {

                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_STOP);
            }
        }
        entity.setCreatedTime(new Date());

        this.updateById(entity);

        return ResponseResult.okResult(entity.getId());
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {

        WmChannel channel = this.getById(id);

        if (channel == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (channel.getStatus() == true) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_DELETE);
        }
        this.removeById(id);

        return ResponseResult.okResult(id);
    }
}
